package com.homwee.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

//zcj 2018-10-31 import com.changhong.inface.net.NetworkSettingUtils;
//zcj 2018-10-31 import com.changhong.inface.net.wifi.WifiAccessPoint;

import com.homwee.factory.util.BarCodeUtils;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;
import com.homwee.factory.util.Contants;

import android.app.Activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.graphics.Color;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.Intent;
import android.widget.BaseAdapter;


import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;
import com.mediatek.twoworlds.tv.MtkTvUtil;

public class FactoryKeypadTestActivity extends Activity {
	public static Context mContext;
	private ListView mListView;
	private static ArrayList<String> mDatas = new ArrayList<String>();
	public final String TAG = "CHFactory.KeyPadTest";
	public int key_count=0;

	MyAdapter adapter;

	public ComSettingManager mcomSettingManager;
	public FactorySetting factorySetting;

	//factory keypad test keycode
	private int keyPadNum=Integer.parseInt(MtkTvFApiInformation.getInstance().getIniParameter(
			Contants.PATH_CUSTOM_FUNC_INI,"KEYPADNUM:keypadNumValue"));
	boolean[] doneTest =new boolean[10];
	boolean ALLDONE=false;
	private final String[] keypad={"Power","Vol -","Up","Down","Vol +","Source","Menu"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// LayoutInflater vi = (LayoutInflater)
		// this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// mRootLayout =
		// (RelativeLayout)vi.inflate(R.layout.menu_information_list,null);
		// setContentView(mRootLayout);
		mContext = this;
		mcomSettingManager=ComSettingManager.getInstance(this);
		factorySetting = mcomSettingManager.getFactory();

		try {
			initData();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

			setContentView(R.layout.menu_information_list);
			mListView = (ListView) findViewById(R.id.listInfoView);
			adapter = new MyAdapter(mContext, mDatas);
			mListView.setAdapter(adapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String mSelName = ((TextView) arg1).getText().toString();
				Log.v("tv", mSelName);

			}
		});


		mListView.setFocusable(false);

	}

	private void initData() throws InterruptedException {
		mDatas.clear();
		for (int i = 0; i < 10; i++) {
			doneTest[i]=false;
		}
		switch(keyPadNum){
			case 1:
				mDatas.add(0,keypad[0]);
				break;
			case 5:
				for(int i=0;i<keyPadNum;i++){
					mDatas.add(i,keypad[i]);
				}
				break;
			case 7:
				for(int i=0;i<keyPadNum;i++){
					mDatas.add(i,keypad[i]);
				}
				break;
			default:
				mDatas.add(0,keypad[0]);
				break;
		}

		Log.d(TAG,"KeyPadNUm: "+MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI,"KEYPADNUM:keypadNumValue"));

		//keypadControl(1);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
			case KeyEvent.KEYCODE_INFO:
				if(ALLDONE){
					finish();
				}else {
					mDatas.set(0, "Power:			OK");
					doneTest[0] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_1:
				if(ALLDONE) {
					finish();
				}else {
					mDatas.set(1, "Vol-:			OK");
					doneTest[1] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_2:
				if(ALLDONE){
					finish();
				}else {
					mDatas.set(2, "Up:			OK");
					doneTest[2] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_3:
				if(ALLDONE){
					finish();
				}else {
					mDatas.set(3, "Down:			OK");
					doneTest[3] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_4:
				if(ALLDONE){
					finish();
				}else {
					mDatas.set(4, "Vol+:			OK");
					doneTest[4] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_5:
				if(ALLDONE){
					finish();
				}else {
					mDatas.set(5, "Source:			OK");
					doneTest[5] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_0:
				if(ALLDONE){
					finish();
				}else {
					mDatas.set(6, "Menu:			OK");
					doneTest[6] = true;
					adapter.notifyDataSetChanged();
				}
				break;
			case KeyEvent.KEYCODE_BACK:
				finish();
				break;
			default:
				Log.d(TAG,"Receive keycode ="+keyCode);
//				mDatas.add(key_count, TAG+"Receive keycode ="+keyCode);
//				key_count++;
//				adapter.notifyDataSetChanged();
				break;
		}
		return super.onKeyDown(keyCode, event);

	}

	protected void onStart() {
		super.onStart();
	}


	@Override
	public void finish() {
		// TODO Auto-generated method stub
		ALLDONE=false;
		//keypadControl(0);
		super.finish();
	}

        
	private class MyAdapter extends BaseAdapter{

		private Context mContext;
		private ArrayList<String> mList = null;
		private final LayoutInflater mListContainer;
		public MyAdapter(Context context, ArrayList<String> list) {
			mContext = context;
			mListContainer = LayoutInflater.from(mContext);
			mList = list;
		}
		@Override
		public long getItemId(int i) {
			return i;
		}
                @Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mList.get(position);
		}
                @Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			String str =(String)getItem(position);
		        convertView = mListContainer.inflate(R.layout.simple_list_item_1, null);
                        TextView tv = (TextView)convertView.findViewById(R.id.tv_text);
                        TextView tv_ret = (TextView)convertView.findViewById(R.id.tv_text_ret);
                        //Log.d(TAG,"tv = "+tv + " convertView "+convertView);

			tv.setText(str);

			if(str.contains("OK")){
				tv.setTextColor(Color.GREEN);
			}else if(str.contains("NG")){
				tv.setTextColor(Color.RED);
			}

			boolean tempb=true;
			for(int i=0;i<keyPadNum;i++){
				tempb=tempb&&doneTest[i];
				//Log.d(TAG,"doneTest"+i+":"+doneTest[i]+";ALLDONE:"+ALLDONE);
			}
			if(tempb){
				ALLDONE=true;
			}

			return convertView;
		}
                
	}

	private int keypadControl(int value){
		int ret=MtkTvUtil.KeypadControl(value);
		Log.d(TAG,"keypadControl="+ret);
		return ret;
	}

        
}
